package com.hekang.dialogdemo;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private AlertDialog dialog;
    private AlertDialog dialog1;
    private Button showStyleAlert, showDialog, btn_showAdapter, btn_size;
    private TextView msg;
    private MyButtonListnenr myButtonListnenr;
    private int ci = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        intiDialog();
        setMyButtonListnenr();
    }


    /**
     * 控件初始化
     */
    public void initView() {
        showStyleAlert = (Button) findViewById(R.id.showStyleAlert);
        showDialog = (Button) findViewById(R.id.showDialog);
        msg = (TextView) findViewById(R.id.msg);
        btn_showAdapter = (Button) findViewById(R.id.btn_showAdapter);
        btn_size = (Button) findViewById(R.id.btn_size);
    }


    /**
     * 绑定按钮
     */
    public void setMyButtonListnenr() {
        myButtonListnenr = new MyButtonListnenr();
        showDialog.setOnClickListener(myButtonListnenr);
        showStyleAlert.setOnClickListener(myButtonListnenr);
        btn_showAdapter.setOnClickListener(myButtonListnenr);
        btn_size.setOnClickListener(myButtonListnenr);
    }


    /**
     * 按钮监听器
     */
    class MyButtonListnenr implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.showDialog:
                    if (dialog != null && !dialog.isShowing()) {
                        dialog.show();
                    }
                    break;
                case R.id.showStyleAlert:
                    showCommonListDialog();
                    break;
                case R.id.btn_showAdapter:
                    showAdapterListDialog();
                    break;
                case R.id.btn_size:
                    showSingleListDialog();
                    break;

            }
        }
    }


    /**
     * 初始化Dialog
     */
    public void intiDialog() {
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //标题
        builder.setTitle("温馨提示");
        //图标
        builder.setIcon(R.mipmap.ic_launcher);
        //普通内容
        builder.setMessage("确定要离开吗？");
        //添加确认按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了确认按钮", Toast.LENGTH_LONG).show();
            }
        });
        //添加取消按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_LONG).show();
            }
        });
        //添加第三个按钮
        builder.setNeutralButton("稍后处理", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了稍后处理按钮", Toast.LENGTH_LONG).show();
            }
        });

        //构造对象
        dialog = builder.create();

    }


    /**
     * 带单选按钮Dialog
     */
    public void showSingleListDialog(){
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //标题
        builder.setTitle("温馨提示");
        //图标
        builder.setIcon(R.mipmap.ic_launcher);
        //还得字体大小
        final String[] fontSize = getResources().getStringArray(R.array.font_size);

        builder.setSingleChoiceItems(R.array.font_name, ci, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ci = which;
                msg.setTextSize(Float.parseFloat(fontSize[which]));
                dialog1.dismiss();
            }
        });

        dialog1 = builder.create();
        dialog1.show();

    }


    /**
     * AlertDialog图片文本列表
     */
    public void showAdapterListDialog() {
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //标题
        builder.setTitle("手机设置");
        //图标
        builder.setIcon(R.mipmap.ic_launcher);
        //获取名称数组
        String[] stringNames = getResources().getStringArray(R.array.setting_names);
        //获取图片数组
        String[] stringImages = getResources().getStringArray(R.array.setting_images);


        //集合
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        //循环读取数据添加到集合
        for (int i = 0; i < stringNames.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            //添加名称
            map.put("setting", stringNames[i]);
            //获取图片资源Id
            int imageId = getResources().getIdentifier(stringImages[i], "mipmap", getPackageName());
            //添加图片
            map.put("image", imageId);

            //添加到集合
            data.add(map);
        }

        //构建SimpleAdapter
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item,
                new String[]{"setting", "image"},
                new int[]{R.id.tv_name, R.id.iv_logo});

        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            Intent intent = null;

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //判断下标
                switch (which) {
                    case 0://设置wifi
                        intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        break;
                    case 1://蓝牙
                        intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
                        break;
                    case 2://设置声音
                        intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
                        break;
                    case 3://设置日期
                        intent = new Intent(Settings.ACTION_DATE_SETTINGS);
                        break;
                }
                startActivity(intent);

            }
        });

        //显示
        builder.create().show();


    }

    /**
     * 普通列表
     */
    public void showCommonListDialog() {
        //构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //标题
        builder.setTitle("设置背景颜色");
        //图标
        builder.setIcon(R.mipmap.ic_launcher);
        //设置样式
        builder.setItems(R.array.colors_name, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        msg.setBackgroundColor(Color.RED);
                        break;
                    case 1:
                        msg.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        msg.setBackgroundColor(Color.BLUE);
                        break;
                }
            }
        });
        //显示
        builder.create().show();
    }


    /**
     * 用户点击了返回键
     */
    @Override
    public void onBackPressed() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
